# Copyright (C) 2006  Apptility LLC  http://www.apptility.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

require 'yaml'
require 'singleton'
require 'logger'

class UIFContainer  
    include Singleton  

    def init_navtabs_w_array navtabarray
      @navtabsarray = navtabarray
    end
  
    def navtabsarray
      @navtabsarray
    end
    
    def init_navtabs_w_hash navtabshash
      @navtabshash = navtabshash
    end

    def navtabshash
      @navtabshash
    end
    
    def init_maintab_order_map main_tab_order_map
      @main_tab_order_map = main_tab_order_map
    end

    def main_tab_order_map
      @main_tab_order_map
    end
    
end  
  
class NavTab
  #attr_accessor :maintabarray, :tabname, :subtabshash
  def initialize tabname, tabhash
    @tabname = tabname
    @maintabarray = tabhash
    @subtabshash = Hash.new
    @maintabarray.each {|subtabelement|
          @subtabshash = subtabelement 
      }  
  end
  
  def get_subtabnames
    return @subtabshash.keys
  end

  def get_subtab_node_hash(subtabname)
    @subtab_node_array = @subtabshash[subtabname]
    @subtab_node_array.each {|subtab_node_element|
        return subtab_node_element
      }  
  end
  
  def subtabshash
    @subtabshash
  end
  
  def tabname
    @tabname
  end
end  

  AjubyLogger.logInfo "[Ajuby-UIF]:Loading UI Framework ..."
  #logger.info "[ODOT-UIF]:Loading UI Framework ..."
  
  @config = YAML::load(File.open("#{RAILS_ROOT}/config/uiframework/uif-navtabs.yml" )) 
  AjubyLogger.logInfo "[Ajuby-UIF]:Mapping Nav Tabs..." 
  #YAML::dump(@config)
  
  tabs_array = Array.new()  
  tabs_hash = Hash.new();
  main_tab_order_map = Hash.new();
  @config.each_key {|key| 
    @subtabnames = @config[key]
      
    tab_order_map = Hash.new();
    maintab = NavTab.new(key.to_s, @subtabnames)
    # Get subtab details
    @subtabnames.each {|subtabelement|
        subtabelement.each_key {|keystn|

          @subtabvalues = subtabelement[keystn]
          @subtabvalues.each {|subtabvalue|
            subtabvalue.each_key{|subtabvaluelement|

              if(keystn=="this.self" and subtabvaluelement=='order')
                @main_tab_order = subtabvalue[subtabvaluelement]
                main_tab_order_map[@main_tab_order]= maintab
              else
                if(subtabvaluelement=='order')
                   @subtab_order = subtabvalue[subtabvaluelement]
                   tab_order_map[@subtab_order] = keystn            
                end
              end  
              
              if(subtabvaluelement=="params")
              #AjubyLogger.logInfo "subtabvalue key/value=>"=>subtabvaluelement
               @params_array = subtabvalue["params"]
               @params_array.each {|params_hash|
                  params_hash.each_key{|params_key|
                    #AjubyLogger.logInfo "++++++++++++++++++++++++++"
                    #AjubyLogger.logInfo "params key/value=>"=>params_key + "/#{params_hash[params_key]}"
                  }
                }
              else
                #AjubyLogger.logInfo "subtabvalue key/value=>"=>subtabvaluelement + "/#{subtabvalue[subtabvaluelement]}" 
              end  
              
            }
          }
        }
    }
    
    # Lets add main tab stuff now
    tabs_array.push(maintab)
    
    # Lets add subtab stuff
    @temp_array = Array.new()
    @temp_array.push(@subtabnames)
    @temp_array.push(tab_order_map)
    tabs_hash[key.to_s] = @temp_array 
  }
  
  # Finally load the Nav Tabs to the UIFramework container
  UIFContainer.instance.init_maintab_order_map(main_tab_order_map)
  UIFContainer.instance.init_navtabs_w_array(tabs_array)
  UIFContainer.instance.init_navtabs_w_hash(tabs_hash)
  

  
 